<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function  MyPromise(fn){
        this.success  = "";
        this.fail = "";
        fn((success)=>{
            this.success = success;
        },(fail)=>{
            this.fail  = fail
        });

    }
    MyPromise.prototype.then = function(fn1,fn2){
        if(this.success){
            fn1(this.success)
        }
        if(this.fail){
            fn2(this.fail);
        }
    }


    //手写一个简单的Promise
    let p = new MyPromise((resolve, reject) => {
        let num = Math.round(Math.random() * 100);
        if (num >= 60) {
            resolve(num + "及格");
        } else {
            reject(num + "不及格");
        }
    })

    p.then((data)=>{
        alert("成功的结果为"+data)
    },(data)=>{
        alert("失败的结果为"+data)
    })

</script>

</html>